layui.use('table', function(){
    var table = layui.table;

    //展示已知数据
    var tableIns = table.render({
        elem: '#appTable'
        ,cols: [[ //标题栏
            {align:'center',type:'numbers',width:80, title: '序号'}
            ,{field: 'name', width:300, title: '应用名称',align:"center"}
            ,{field: 'services', title: '服务实例'}
            ,{field: 'status',width:100, title: '状态',align:"center"}
            ,{width:300,align:"center",title: '操作', toolbar: '#handler'}

        ]]
        ,data:[]
    });

    $.get(contextRoot+"eureka/apps",function (result) {
        var data = [];

        for (var i in result){
            var instArr = [];
            var instance = result[i].instance
            for (var j in instance){
                instArr.push(instance[j].instanceId)
            }
            var services = instance.length + "(" + instArr.join(",")+")";
            data.push({
                name: result[i].name,
                services: services,
                status: instance.length>0?instance[0].status:"DOWN",
            })
        }

        //这里以搜索为例
        tableIns.reload({
            data:data
        });
    })

    //监听行工具事件
    table.on('tool(appTable)', function(obj){
        var data = obj.data;
        if(obj.event === 'gatewayConfig'){
            layer.open({
                title: '网关配置'
                ,type: 2
                ,area: ['800px', '80%']
                ,content: contextRoot+"gateway/form?routeId="+data.name.toLowerCase()
            })
        } else if(obj.event === 'monitor'){

        }
    });
});